//
//  MainViewController.m
//  UI9.lesson.fenqu
//
//  Created by dllo on 15/8/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MainViewController.h"
#import "NSString+Characters.h"
@interface MainViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSMutableDictionary *dic;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSString *path  = [[NSBundle mainBundle]pathForResource:@"student" ofType:@"plist"];
    
    self.arr = [NSMutableArray arrayWithContentsOfFile:path];
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.dic = [NSMutableDictionary dictionary];
    
    for (NSMutableDictionary *dic in self.arr) {
        NSString *first = [[dic objectForKey:@"name"]firstCharacterOfName];
        if (![self.dic objectForKey:first]) {
            NSMutableArray *array = [NSMutableArray arrayWithObject:[dic objectForKey:@"name"]];
            [self.dic setObject:array forKey:first];
        } else {
            [[self.dic objectForKey:first] addObject:[dic objectForKey:@"name"]];
            
        }
        }
    
    self.arr = [NSMutableArray arrayWithArray:[self.dic allKeys]];
    [self.arr sortUsingSelector:@selector(compare:)];
    
  }



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reduce";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]autorelease];
    }
  
    NSArray *array = [self.dic objectForKey:[self.arr objectAtIndex:indexPath.section]];
   
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.dic objectForKey:[self.arr objectAtIndex:section]]count];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arr.count;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [NSString stringWithFormat:@"%@",[self.arr objectAtIndex:section]];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
